require('./reset.css')
require('./register.less')
require('../../common/header/index.js')
var _account_service=require('service/account-service.js')
var _util=require('../../../util/util.js')

var _register={
  validation: {
    username: false,
    password: false,
    code: false
  },
  init:function(){
      this.loadInfo();
      this.bindEvents();
      return this;
  },

  loadInfo:function(){
    //初次加载页面时获取验证图片：
    $('#code_image').attr('src','http://localhost:8088/user/kaptcha')
  },

  bindEvents:function(){
    var _this=this;
    //点击刷新验证码：
    $('#code_image').on('click',function(){
      _this.updateKaptcha();
    })

    //所有框提供失去焦点事件：
    $('#username').on('blur',function () {
        _this.checkUsername();
    });
    $('#pwd').on('blur',function () {
      _this.checkPassword();
    });
    $('#repeatedPwd').on('blur',function () {
      _this.checkPassword();
    });
    $('#code').on('blur',function () {
      _this.checkCode();
    });

    //绑定点击提交事件：
    $('#newAccount').on('click',function () {
      if(_this.validation.username && _this.validation.password && _this.validation.code){
        let data={
          username:$.trim($('#username').val()),
          password:$.trim($('#pwd').val()),
          code:$.trim($('#code').val())
        }
        _account_service.register(
          data,
          //注册成功
          res => {
            window.location.href='../../../view/catalog/catalog-main.html'
          },
          //注册失败
          errMsg => {
            _this.updateKaptcha();
            if(errMsg.status == 1)
              $('#usernameMsg').text(errMsg.msg);
            else if(errMsg.status == 101)
              $('#codeMsg').text(errMsg.msg);
          }
        )
      }
    })
  },

  //校验用户名
  checkUsername: function(){
    let username = $('#username').val();
    if(!username){
      $('#usernameMsg').text('用户名不能为空！');
      this.validation.username = false;
    }
    //向服务器请求用户名是否已经存在
    else{
      _account_service.checkUsername(
        username,
        //用户名已经存在
        res => {
          $('#usernameMsg').text('用户名被占用！');
          this.validation.username = false;
        },
        //用户名不存在
        errMsg => {
          $('#usernameMsg').text('');
          this.validation.username = true;
        }
      );
    }   
  },

  //校验密码
  checkPassword: function(){
    let repeatedPassword=$('#repeatedPwd').val();
    let password=$('#pwd').val();
    if(!password){
      $('#pwdMsg').text('密码不能为空！');
      this.validation.password = false;
    }
    else{
      $('#pwdMsg').text('');
      if(repeatedPassword !== password){
        $('#repeatedPwdMsg').text('两次输入的密码不一致！')
        this.validation.password = false;
      }     
      else{
        $('#repeatedPwdMsg').text('');
        this.validation.password = true;
      }
    }
  },

  //校验验证码
  checkCode: function(){
    let code = $('#code').val();
    if(!code){
      $('#codeMsg').text('请输入验证码！')
      this.validation.code = false;
    }
    else{
      $('#codeMsg').text('')
      this.validation.code = true;
    }
  },

  updateKaptcha:function(){
      $('#code_image').attr('src',"http://localhost:8088/user/kaptcha");
  }

}

module.exports=_register.init()
